package projects.ufam.nodes.nodeImplementations;

import java.awt.Color;
import java.awt.Graphics;

import projects.defaultProject.nodes.messages.StringMessage;
import projects.defaultProject.nodes.timers.MessageTimer;
import projects.defaultProject.nodes.timers.SleepTimer;
import projects.defaultProject.nodes.timers.WakeupTimer;
import sinalgo.configuration.WrongConfigurationException;
import sinalgo.gui.transformation.PositionTransformation;
import sinalgo.nodes.Node;
import sinalgo.nodes.NodeState;
import sinalgo.nodes.messages.Inbox;
import sinalgo.nodes.messages.Message;

public class BroadcastNode extends Node {
	int roundsOn = 0;
	int roundsOff = 0;
	int sent = 0;
	int received = 0;

	@Override
	public String toString() {
		return "" + this.ID;
	}

	@Override
	public void handleMessages(Inbox inbox) {
		for (Message m : inbox) {
			received++;
		}
	}

	@Override
	public void checkRequirements() throws WrongConfigurationException {
		// TODO Auto-generated method stub
	}

	@Override
	public void init() {
		this.setEnergyLevel(1.0);
	}

	@Override
	public void neighborhoodChange() {
		// TODO Auto-generated method stub
	}

	@Override
	public void postStep() {
		// Broadcast frequency reduces as the battery wears down
		SleepTimer st = new SleepTimer();
		WakeupTimer wt = new WakeupTimer();
		int r = this.ID % 10;
		if (this.ID % 10 == 1) {
			this.broadcast(0);
		} else {
			if (getState().isEnabled()) {
				st.startRelative(1, this);
				wt.startRelative(r+1, this);
			}
		}
	}

	@Override
	public void preStep() {
	}

	public void draw(Graphics g, PositionTransformation pt, boolean highlight) {
		super.draw(g, pt, highlight);
	}

	public void broadcast(int time) {
		// Only even ids will actually broadcast
		StringMessage sm = new StringMessage("Broadcast!");
		if (time == 0) {
			this.broadcast(sm);
		} else {
			MessageTimer mt = new MessageTimer(sm);
			mt.startRelative(time, this);
		}
	}

	public int getSent() {
		return sent;
	}

	public void setSent(int sent) {
		this.sent = sent;
	}

	public int getReceived() {
		return received;
	}

	public void setReceived(int received) {
		this.received = received;
	}

	@Override
	public void wakeUp() {
		roundsOn = 0;
	}

	public void sleep() {
		roundsOff = 0;
	}
}

/*
 * Parâmetros dos experimentos
 * 
 * RPE -project rpe -gen 2000 rpe:RPENode Random C=StaticUDG -overwrite
 * javaVMmaxMem=900 dimensions=3 dimX=100 dimY=100 dimZ=2 UDG/rMax=10
 * RPE/Beacons/percentage=0.05 RPE/MinReferences/number=4
 * RPE/MaxReferences/number=10 RPE/MaxResidual/number=0.01
 * Noise/Mean/percentage=0.001 Noise/Variance/percentage=0.01 -project masters
 * -gen 1000 masters:RPENode Random C=StaticUDG -overwrite javaVMmaxMem=900
 * dimZ=2 UDG/rMax=10 RPE/Beacons/percentage=0.05 RPE/MinReferences/number=4
 * RPE/MaxReferences/number=10 RPE/MaxResidual/number=0.01
 * Noise/Mean/percentage=0.001 Noise/Variance/percentage=0.01
 * 
 * 
 * DPE -project rpe -gen 150 rpe:RPENode Random C=StaticUDG -overwrite
 * javaVMmaxMem=900 dimensions=2 dimX=70 dimY=70 dimZ=0 UDG/rMax=15
 * RPE/Beacons/percentage=0.05 RPE/MinReferences/number=3
 * RPE/MaxReferences/number=100 RPE/MaxResidual/number=20
 * Noise/Mean/percentage=0 Noise/Variance/percentage=0.1 java -cp binaries/bin
 * sinalgo.Run -batch -project rpe -gen 150 rpe:RPENode Random C=StaticUDG
 * -overwrite javaVMmaxMem=900 dimensions=2 dimX=70 dimY=70 dimZ=0 UDG/rMax=15
 * RPE/Beacons/percentage=0.05 RPE/MinReferences/number=3
 * RPE/MaxReferences/number=100 RPE/MaxResidual/number=20
 * Noise/Mean/percentage=0 Noise/Variance/percentage=0.1
 */